import 'dart:async';
import 'package:aqua_plunge/http/app_manager.dart';
import 'package:flutter/material.dart';
import 'login_page.dart';
import 'tab_navigation_page.dart';

/// 引导页
class LeadPage extends StatefulWidget {
  const LeadPage({super.key});

  @override
  State<LeadPage> createState() => _LeadPageState();
}

class _LeadPageState extends State<LeadPage> {
  int _currentPage = 0;

  final List<String> _bgimgs = [
    "images/img_lead1.png",
    "images/img_lead2.png",
    "images/img_lead3.png",
  ];

  final List<String> _describes = [
    "Explore underwater wonders. Share photos, join events, and connect with divers.",
    "Discover photos, events, and stories. Follow, share, and dive in!",
    "Dive into the diving community. Share, support, and stay updated on diving trends."
  ];

  final List<String> _btnimgs = [
    "images/icon_btn_next.png",
    "images/icon_btn_next.png",
    "images/icon_btn_start.png",
  ];

  late PageController _pageController;

  void _nextPage() {
    if (_currentPage < 2) {
      _pageController.animateToPage(_currentPage + 1,
          duration: const Duration(milliseconds: 300), curve: Curves.easeIn);
    }
  }

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentPage);
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      if (_currentPage < 2) {
        Timer.periodic(const Duration(seconds: 2), (t) {
          _nextPage();
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return PageView.builder(
        controller: _pageController,
        itemCount: _bgimgs.length,
        itemBuilder: (
          context,
          index,
        ) {
          _currentPage = index;
          return _welcomeView();
        });
  }

  Widget _background() {
    return Positioned.fill(
        child: Image.asset(
      _bgimgs[_currentPage],
      fit: BoxFit.fill,
    ));
  }

  Widget _describe() {
    return Positioned(
        bottom: 170,
        left: 0,
        right: 0,
        child: Container(
          padding: const EdgeInsets.all(20),
          child: Text(
            _describes[_currentPage],
            style: const TextStyle(
                color: Colors.white,
                fontSize: 20,
                decoration: TextDecoration.none),
            textAlign: TextAlign.left,
          ),
        ));
  }

  Widget _bottomBtn() {
    return Positioned(
        bottom: 70,
        left: 0,
        right: 0,
        child: GestureDetector(
          onTap: _currentPage < 2 ?_nextPage:_toLoginPage,
          child: Image.asset(
            _btnimgs[_currentPage],
            width: 290,
            height: 55,
          ),
        ));
  }

  void _toLoginPage(){
    Navigator.of(context).pushReplacement(
        MaterialPageRoute(builder: (context) => const LoginPage()));
  }

  Widget _welcomeView() {
    return Stack(
      children: [_background(), _describe(), _bottomBtn()],
    );
  }
}
